<template>
  <ul v-if="infoFlag">
    <li>姓名：{{ ruleForm.name }}</li>
    <li>性别：{{ ruleForm.sex }}</li>
    <li>地址：{{ ruleForm.region }}</li>
    <li>日期：{{ ruleForm.date }}</li>
    <li>兴趣爱好： <template v-for="(item, index) in ruleForm.xiGuan" :key="index"><span style="margin-right: 10px;">{{ item }}</span></template></li>
  </ul>
  <div class="form_box">
    <el-form 
      ref="ruleFormRef" 
      :model="ruleForm" 
      :rules="rules"
      :size="formSize"
      label-width="auto" 
      label-position="left"
      class="demo-ruleForm" 
      style="max-width: 600px;" 
      status-icon
    >

      <el-form-item label="用户名" prop="name">
        <el-input v-model="ruleForm.name" />
      </el-form-item>

      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="ruleForm.sex">
          <el-radio label="男" value="male">男</el-radio>
          <el-radio label="女" value="female">女</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="地址" prop="region">
        <el-select v-model="ruleForm.region" placeholder="请选择活动区域">
          <el-option v-for="item in regionData" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="活动日期" prop="date">
        <el-date-picker 
          v-model="ruleForm.date" 
          type="date" 
          placeholder="请选择日期"
          style="width: 100%;"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>

      <el-form-item label="兴趣爱好" prop="xiGuan">
        <el-checkbox-group v-model="ruleForm.xiGuan">
          <el-checkbox v-for="item in xiGuan" :key="item.value" :label="item.label" :value="item.value">{{ item.label }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>

      <!-- 按钮 -->
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">提交</el-button>
        <el-button type="" @click="resetForm(ruleFormRef)">重置</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
const formSize = ref('default');
const ruleFormRef = ref();
const infoFlag = ref(false);
const regionData = reactive([
  {
    value: '上海',
    label: '上海'
  },
  {
    value: '北京',
    label: '北京'
  },
  {
    value: '广州',
    label: '广州'
  }
]);

const xiGuan = reactive([
  {
    value: 'lanqiu',
    label: '篮球'
  },
  {
    value: 'zuqiu',
    label: '足球'
  },
  {
    value: 'yumaoqiu',
    label: '羽毛球'
  }
]);

const ruleForm = reactive({
  name: '',
  sex: 'male',
  region: '',
  date: '',
  xiGuan: []
});

const rules = reactive({
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    { min: 3, max: 6, message: '输入的字符长度在3～15个之内', trigger: 'blur' }
  ],
  region: [
    { required: true, message: '请选择地址', trigger: 'change' }
  ],
  date: [
    { required: true, message: '请选择日期', trigger: 'change' }
  ],
  xiGuan: [
    { required: true, message: '请选择兴趣爱好', trigger: 'change' }
  ]
});

// 提交表单
const submitForm = async (formEl) => {
  if(!formEl) return;
  await formEl.validate((valid, fields) => {
    if(valid) {
      console.log('submit!', valid);
      console.log('提交后的数据=====>', ruleForm);
      infoFlag.value = true;
    }else {
      console.log('error submit!', fields);
    }
  })
};

// 重置表单
const resetForm = (formEl) => {
  if(!formEl) return;
  formEl.resetFields();
  infoFlag.value = false;
}

</script>

<style scoped>

.form_box {
  position: relative;
}

</style>

